<?php

namespace App\Module\Teacher\Service;

use App\Constants\Constant;
use App\Exception\AppException;
use App\Model\Payment;

class PayService {

    public function search($pay_id,$platform_trade_no,$order_status,$pagesize){
        $query = Payment::query()->latest("pay_id")->where("pay_status",Constant::PAY_STATUS_SUCCESS);

        if(!empty($pay_id)){
            $query->where("pay_id",$pay_id);
        }
        if(!empty($platform_trade_no)){
            $query->where("platform_trade_no",$platform_trade_no);
        }
        if(!empty($order_status)){
            $query->where("order_status",$order_status);
        }

        return $query->paginate($pagesize);
    }

    public function use($id){
        $model = Payment::query()->findOrFail($id);

        if($model->getAttribute("order_status") != Constant::ORDER_STATUS_UNUSE){
            throw new AppException("订单状态错误,无法核销");
        }

        $model->setAttribute("order_status",Constant::ORDER_STATUS_USE);
        return $model->save();
    }
}